package com.example.service.collar.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.exception.InsertException;
import com.example.mapper.CollarMapper;
import com.example.mapper.PetMapper;
import com.example.mapper.PetTemperatureLimitMapper;
import com.example.mapper.RailingMapper;
import com.example.pojo.Collar;
import com.example.pojo.CollarMsg;
import com.example.pojo.PetTemperatureLimit;
import com.example.pojo.Railing;
import com.example.service.collar.RailingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author w
 */
@Service
public class RailingServiceImpl implements RailingService {
    @Autowired
    RailingMapper railingMapper;
    @Autowired
    CollarMapper collarMapper;
    @Autowired
    PetMapper petMapper;
    @Autowired
    PetTemperatureLimitMapper petTemperatureLimitMapper;

    @Override
    public int insert(Railing railing) {
        if (railing.getPetId()==0||railing.getRailingLatitude()==0||railing.getRailingLongitude()==0||railing.getRailingRadius()==0){
            return 0;
        }
        if (petMapper.selectById(railing.getPetId())==null){
            return 0;
        }
        int insert = railingMapper.insert(railing);
        if (insert!=1){
            throw new InsertException("电子围栏插入失败");
        }
        return railing.getRailingId();
    }

    @Override
    public Railing isOutOfBounds(CollarMsg collarMsg, String collarId) {
        double latitude = collarMsg.getLatitude();
        double longitude = collarMsg.getLongitude();
        Collar collar = collarMapper.selectById(collarId);
        QueryWrapper<Railing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pet_id",collar.getPetId());
        List<Railing> railings = railingMapper.selectList(queryWrapper);
        if (railings.size()>0){
            for (Railing railing : railings
            ) {
                double railingRadius = railing.getRailingRadius();
                double railingLatitude = railing.getRailingLatitude();
                double railingLongitude = railing.getRailingLongitude();
                double distant = distance(latitude,longitude,railingLatitude,railingLongitude);
                if (distant>railingRadius){
                    return railing;
                }
            }
        }
        return null;
    }

    @Override
    public boolean update(Railing railing) {
        int i = railingMapper.updateById(railing);
        return i==1;
    }

    /**
     * 判断项圈是否超过限制
     *
     * @param collarId  项圈id
     * @param collarMsg 温度信息
     * @return 是否警告
     */
    @Override
    public boolean isOutTemperatureLimit(String collarId, CollarMsg collarMsg) {
        Collar collar = collarMapper.selectById(collarId);
        if(collar==null){
            System.out.println("collarId不存在！："+collarId);
            return false;
        }
        int petId = collar.getPetId();
        PetTemperatureLimit temperatureLimit = petTemperatureLimitMapper.selectOne(new QueryWrapper<PetTemperatureLimit>().eq("pet_id", petId));
        if (temperatureLimit!=null){
            double limitMax = temperatureLimit.getLimitMax();
            double limitMin = temperatureLimit.getLimitMin();
            double temp = collarMsg.getTemp();
            return temp > limitMax || temp < limitMin;
        }else {
            return false;
        }
    }


    double distance(double lat1, double lon1, double lat2, double lon2)
    {
        double theta = lon1 - lon2;
        double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
                + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
                * Math.cos(deg2rad(theta));
        dist = Math.acos(dist);
        dist = rad2deg(dist);
        return dist * 60 * 1.1515;
    }
    //将角度转换为弧度
    static double deg2rad(double degree)
    {
        return degree / 180 * Math.PI;
    }
    //将弧度转换为角度
    static double rad2deg(double radian)
    {
        return radian * 180 / Math.PI;
    }


    /**
     * 设置监听宠物温度上下限
     *
     * @param petTemperatureLimit 温度上下限信息，包括宠物id
     * @return 是否成功
     */
    @Override
    public boolean setPetTemperatureLimit(PetTemperatureLimit petTemperatureLimit) {
        int insert = petTemperatureLimitMapper.insert(petTemperatureLimit);
        return insert==1;
    }
}
